//
//  WBTopView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBTopView.h"
#import <Masonry.h>
#import "UILabel+WBLabel.h"
#import <UIImageView+WebCache.h>
#import "WBStatusViewModel.h"
#import "WBStatusPictureView.h"


@interface WBTopView ()

//头像
@property (nonatomic , strong) UIImageView *headImageView;
//姓名
@property (nonatomic , strong) UILabel *nameLabel;

//会员等级图片
@property (nonatomic , strong) UIImageView *membershipImageView;

//时间label
@property (nonatomic , strong) UILabel *timeLabel;

//来源label
@property (nonatomic , strong) UILabel *sourceLabel;

//认证图片
@property (nonatomic , strong) UIImageView *verifiedImageView;

//微博正文
@property (nonatomic , strong) UILabel *contentLabel;

//微博配图
@property (nonatomic , strong) WBStatusPictureView *pictureView;

//记录约束的属性
@property (nonatomic , strong) MASConstraint *topViewBottomConstraints;

//时间格式化
@property (nonatomic , strong) NSDateFormatter *formatter;

@end

@implementation WBTopView

#pragma mark - 重写set方法给控件赋值
- (void)setStatusViewModel:(WBStatusViewModel *)statusViewModel
{
    _statusViewModel = statusViewModel;
    
    //通过sdweb 给头像赋值 异步下载网络图片
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:statusViewModel.statusModel.user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default"]];
    
    //给名字赋值
    self.nameLabel.text = statusViewModel.statusModel.user.name;
    
    //给会员等级图片赋值
    self.membershipImageView.image = statusViewModel.membershipImage;
    
    //给认证图片赋值
    self.verifiedImageView.image = statusViewModel.verifiedImage;
    
    //给微博正文赋值
    self.contentLabel.text = statusViewModel.statusModel.text;
    
    //给来源赋值
    self.sourceLabel.text = statusViewModel.sourceStr;
    
    //给时间赋值
    self.timeLabel.text = statusViewModel.sinaTime;
    
    //判断有没有配图  配图使用collectionview做
    //卸载约束
    [self.topViewBottomConstraints uninstall];
    
    if(statusViewModel.statusModel.pic_urls.count > 0){
        //代表有配图
        //赋值
        self.pictureView.pictInfos = statusViewModel.statusModel.pic_urls;
        
        //更新约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.topViewBottomConstraints = make.bottom.mas_equalTo(self.pictureView).offset(10);
        }];
        //显示配图
        self.pictureView.hidden = NO;
        
    }else{
        //代表没有配图
        //更新约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.topViewBottomConstraints = make.bottom.mas_equalTo(self.contentLabel).offset(10);
        }];
        //隐藏配图
        self.pictureView.hidden = YES;
        
    }
    
}

- (NSDateFormatter *)formatter
{
    if(!_formatter)
    {
        _formatter = [[NSDateFormatter alloc] init];
    }
    
    return _formatter;
}

#pragma mark - 懒加载控件
- (UIImageView *)headImageView
{
    if(!_headImageView)
    {
        _headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default"]];
        
    }
    
    return _headImageView;
    
}



- (UILabel *)nameLabel
{
    if(!_nameLabel)
    {
        
        _nameLabel = [[UILabel alloc] initWithFont:15 andTextColor:[UIColor darkGrayColor]];
        
    }
    
    return _nameLabel;
}

- (UIImageView *)membershipImageView
{
    if(!_membershipImageView)
    {
        _membershipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership"]];
    }
    return _membershipImageView;
}

- (UILabel *)timeLabel
{
    if(!_timeLabel)
    {
        _timeLabel = [[UILabel alloc] initWithFont:10 andTextColor:AppColor];
        
        _timeLabel.text = @"时间";
        
    }
    return _timeLabel;
}

- (UILabel *)sourceLabel
{
    if(!_sourceLabel)
    {
        _sourceLabel = [[UILabel alloc] initWithFont:10 andTextColor:[UIColor darkGrayColor]];
        
        _sourceLabel.text = @"来源";
        
    }
    
    return _sourceLabel;
}

- (UIImageView *)verifiedImageView
{
    if(!_verifiedImageView)
    {
        _verifiedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_vgirl"]];
        
    }
    
    return _verifiedImageView;
    
}

- (UILabel *)contentLabel
{
    if(!_contentLabel)
    {
        
        _contentLabel = [[UILabel alloc] initWithFont:15 andTextColor:[UIColor blackColor] andMaxWidth:kScreenW - 20];
        
        
    }
    
    return _contentLabel;
    
}

- (WBStatusPictureView *)pictureView
{
    if(!_pictureView)
    {
        
        _pictureView = [[WBStatusPictureView alloc] init];
    }
    
    return _pictureView;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置UI
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    //添加控件
    [self addSubview:self.headImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.membershipImageView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.verifiedImageView];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    //设置约束
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //上 左 距离10 大小35 * 35
        make.top.leading.equalTo(self).offset(10);
        make.size.mas_equalTo(CGSizeMake(35, 35));
        
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //左边 头像 10 顶部和头像一样
        make.leading.mas_equalTo(self.headImageView.mas_trailing).offset(10);
        make.top.mas_equalTo(self.headImageView);
        
    }];
    [self.membershipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //左边距离namelabel 10 Y中心一样
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(10);
        make.centerY.mas_equalTo(self.nameLabel);
        
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //左边和namelabel一样 底部和headimage一样
        make.leading.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.headImageView);
        
    }];
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //左边距离timelabel 10 底部和timelabel一样
        make.leading.mas_equalTo(self.timeLabel.mas_trailing).offset(10);
        make.bottom.mas_equalTo(self.timeLabel);
        
    }];
    [self.verifiedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.headImageView.mas_trailing);
        make.centerY.mas_equalTo(self.headImageView.mas_bottom);
        
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //顶部 等于 headIMageView的底部 + 10 左边距离10
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(10);
        make.leading.mas_equalTo(self).offset(10);
    }];
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10);
        // make.size.mas_equalTo(CGSizeMake(100, 100));
        make.leading.mas_equalTo(self).offset((kScreenW-250)*0.5);
        
    }];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //标记约束
        self.topViewBottomConstraints = make.bottom.mas_equalTo(self.pictureView.mas_bottom).mas_offset(10);
    }];
    
}


@end
